/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager2ndedition.server;

import taskmanager2ndedition.User;
import taskmanager2ndedition.exceptions.ObjectNotFoundException;
import taskmanager2ndedition.exceptions.UserNotFoundException;
import java.util.LinkedList;
import java.util.List;
import taskmanager2ndedition.ClientUser;
import taskmanager2ndedition.ClientUserInfo;
import taskmanager2ndedition.ClientUserInfo.UserState;

/**
 *
 * @author denaas
 */
public class UserManager {
    private Storage<User> storage;
    private int currentUserId = 0; 
    
    public UserManager(Storage<User> storage){
        this.storage = storage;
        
        int maxId = 0;
        List<User> list = storage.getAll();
        for(User user: list){
            maxId = maxId < user.getId() ? user.getId() : maxId;
        }
        currentUserId = maxId + 1;
    }
    
    public User addUser(String login, String password){
        User user = new User(login, password.hashCode(), false,currentUserId);
        user.setState(UserState.Added);
        user.setModified(true);
        currentUserId++;
        storage.add(user);
        return user;
    }
    
    public void deleteUser(int userId) throws UserNotFoundException{
        try{
             User user  = findById(userId);
             user.setState(UserState.Removed);
             user.setModified(true);
             storage.update(user);
            //storage.remove(userId);     
        }
        catch(ObjectNotFoundException e){
            throw new UserNotFoundException();
        }
    }
    
    public void editUser(int userId, String password) throws UserNotFoundException{
        User user  = findById(userId);
        if (password != null && password.hashCode() != user.getPassword())
            user.setPassword(password.hashCode());
        storage.update(user);
    }
    
    public User findById(int userId) throws UserNotFoundException{
        try{
            User user = storage.get(userId);
            if (user.getState() == UserState.Removed)
                throw new ObjectNotFoundException();
            return user;
        }
        catch(ObjectNotFoundException e){
            throw new UserNotFoundException();
        }
    }
    
    public User findByLogin(String login) throws UserNotFoundException{
        User theUser = null;
        for (User user: storage.getAll()){
            if (user.getLogin().equals(login)){
                theUser = user;
                break;
            }
        }
        if (theUser == null || theUser.getState() == UserState.Removed){
            throw new UserNotFoundException();
        }
        return theUser;
    }
    
    public List<ClientUser> getAll(){
        List<User> list  = storage.getAll();
        List<ClientUser> result = new LinkedList<ClientUser>();
        for(User user: list){
            ClientUser clientUser = new ClientUser();
            clientUser.setId(user.getId());
            clientUser.setLogin(user.getLogin());
            clientUser.setState(user.getState());
            result.add(clientUser);
        }
        return result;
       // return storage.getAll();
    }
    
    public List<ClientUser> getModified(){
        
        List<ClientUser> modifiedUsers = new LinkedList<ClientUser>();
        for (User user: storage.getAll()){
            if (user.isModified()){
                ClientUser clientUser = new ClientUser();
                clientUser.setId(user.getId());
                clientUser.setLogin(user.getLogin());
                clientUser.setState(user.getState());
                modifiedUsers.add(clientUser);
                user.setModified(false);
                storage.update(user);
            }
        }
        return modifiedUsers;
          
         /*/
         List<ClientUserInfo> modifiedUsers = new LinkedList<ClientUserInfo>();
        for (User user: storage.getAll()){
            if (user.isModified()){
                modifiedUsers.add((ClientUserInfo)user);
                user.setModified(false);
                storage.update(user);
            }
        }
        return modifiedUsers;
          * 
          */
    }
    
}
